package com.example.demo.Service;

import com.example.demo.Model.User;
import com.example.demo.Repository.UserRepository;
import com.example.demo.util.Comm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserRepository userRepository;
    @Override
    public User findbyId(String id) {
        return userRepository.findById(id).orElse(null);
    }
    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }
    @Override
    public String save(User ayUser) {
        User vUser=userRepository.findByUsername((ayUser.getUsername().toString()));
        if(vUser == null){
            userRepository.save(ayUser);
            return "注册成功";
        }else{
            return "用户已存在";
        }

    }
    @Override
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    @Override
    public String verifyLogin(User user){
        User gUser= userRepository.findByUsername((user.getUsername().toString()));
        if(gUser!=null){
            if(gUser.getPassword().equals(user.getPassword())){

                if (gUser.getUseright().equals("1")){
                    return "1";
                }else {
                    return "2";
                }
            }else{
                return "密码错误";
            }
        }else {
            return "用户不存在";
        }
    }

//    @Override
//    public boolean verifyEmail(User user){
//        User gUser= userRepository.findByUsername((user.getUsername().toString()));
//        if(gUser!=null){
//            if(){
////                    gUser.getUsercheck().equals(user.getUsercheck())){;
////                gUser.setUsercheck("");
//                userRepository.save(gUser);
//                return true;
//            }else{
//                return false;
//            }
//        }else {
//            return false;
//        }
//    }

    @Override
    public User findbyName(String username) {
        return userRepository.findByUsername(username);
    }

}
